﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MinhTam.DataAccess.Web;



public partial class bvchitiet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hienthi_thongtin();

        }
    }
    public void hienthi_thongtin()
    {
        try
        {


            string loaibaiviet = Request.QueryString["req"];
            string bvid = Request.QueryString["id"];

            String sql = @"select top 1 b.ngay_tao, a.ten_loai,b.bai_viet_id,b.ten_bai_viet,b.noi_dung,b.HINH_THUMBNAIL,b.loai_bai_viet
                        from  bai_viet b 
                        left join loai_bai_viet a  on a.loai_bai_viet_id = b.loai_bai_viet
                        where b.duyet='Y' ";

            if (!string.IsNullOrEmpty(loaibaiviet))
            {
                sql += "and a.loai_bai_viet_id=" + loaibaiviet;
            }
            if (!string.IsNullOrEmpty(bvid))
            {
                sql += " and b.bai_viet_id=" + bvid;
            }
            sql += " ORDER BY ISNULL (b.NOI_BAT,'N') DESC,b.NGAY_TAO ASC";
            DataTable table = SQLConnectWeb.GetData(sql);


            if (table == null || table.Rows.Count == 0 || table.Rows[0]["ten_bai_viet"].ToString() == "")
            {
                noidung.Text = "<span class='format-txt'>KHÔNG TÌM THẤY BÀI VIẾT THEO YÊU CẦU</span>";
                rowHide.Visible = false;
                rowDS.Visible = false;
                return;

            }
            else
            {
                if (!string.IsNullOrEmpty(bvid))
                {
                    url.HRef = "bvall.aspx?req=" + table.Rows[0]["loai_bai_viet"];
                }




                bvid = table.Rows[0]["bai_viet_id"].ToString();
                loaibaiviet = table.Rows[0]["loai_bai_viet"].ToString();
                chude.Text = table.Rows[0]["ten_loai"].ToString();
                tuade.Text = table.Rows[0]["ten_bai_viet"].ToString();
                noidung.Text = table.Rows[0]["noi_dung"].ToString();
                if (table.Rows[0]["ngay_tao"].ToString() != "")
                {
                    DateTime dates = (DateTime)table.Rows[0]["ngay_tao"];
                    ngaydang.Text = dates.ToString("dd/MM/yyyy");
                }

                String sqls = string.Format(@"select top {0} bai_viet_id,ten_bai_viet,loai_bai_viet 
                                    from bai_viet a where a.duyet='Y' and bai_viet_id <> {1} and a.loai_bai_viet={2}
                                    ORDER BY ISNULL(a.NOI_BAT,'N') DESC, a.NGAY_TAO ASC", HelpConstEnum.NUMBER_READMORE_DETAIL, string.IsNullOrEmpty(bvid) ? "-1" : bvid, loaibaiviet);
                DataTable tables = SQLConnectWeb.GetData(sqls);
                if (tables != null && tables.Rows.Count > 0)
                {
                    bvlienquan.DataSource = tables;
                    bvlienquan.DataBind();

                }
                else
                    rowHide.Visible = false;
            }
            if (loaibaiviet == HelpConstEnum.LoaiBaiViet.DUA_DON_TAN_NOI.GetHashCode().ToString())
            {
                string sqlTT = @"SELECT tt.*
                FROM TINH_THANH tt
                WHERE EXISTS(SELECT TOP 1 ks.DIA_DIEM_ID FROM DIA_DIEM_DUA_RUOC_MIEN_PHI ks WHERE ks.TINH_THANH=tt.TINH_THANH_ID) order by isnull(sap_xep,9999)";
                DataTable dt = SQLConnectWeb.GetTable(sqlTT);
                list.DataSource = dt;
                list.DataBind();
            }
            else rowDS.Visible = false;

        }
        catch
        {
        }

    }
    protected void list_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView view = e.Item.DataItem as DataRowView;
        GridView grid = e.Item.FindControl("gridViewData") as GridView;
        if (grid != null)
        {
            string sql = string.Format(@"SELECT ROW_NUMBER() OVER (ORDER BY dddrmp.TEN_DIA_DIEM) STT,dddrmp.*          
            FROM DIA_DIEM_DUA_RUOC_MIEN_PHI dddrmp  where dddrmp.TINH_THANH={0}", view["TINH_THANH_ID"]);
            DataTable dt = SQLConnectWeb.GetTable(sql);
            grid.DataSource = dt;
            grid.DataBind();

        }
    }
}